﻿namespace Softserve.Pottery.Infrastructure              
{
    using System;
    using Softserve.Pottery.Infrastructure.Interfaces;

    public class MessengerControllerBase : IMessenger
    {       

        public MessengerControllerBase(ScopeTypes scope)
        {
            m_Scope = scope;
            Messenger.Register(new Messenger.MessadgeHandler(ResiveMessage));
        }

        #region IMessenger Members

        public bool ValidateMessage(MyMessage msg)
        {
            if (msg == null)
            {
                throw new NullReferenceException();
            }
            if(msg.Scope == m_Scope)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Sd()
        {
            
        }

        /// <summary>
        /// TODO : DeSerialization
        /// </summary>
        /// <param name="msg"></param>
        public void ResiveMessage(MyMessage msg)
        {
            if (ValidateMessage(msg))
            {
                Console.WriteLine(msg.Message);
            }
        }

        /// <summary>
        /// TODO : Serialization
        /// </summary>
        /// <param name="item"></param>
        /// <param name="msg"></param>
        public void SendMessage(ScopeTypes item, string msg)
        {
            Messenger.Add(new MyMessage(item, msg));
        }

        public ScopeTypes GetScopeId
        {
            get { return m_Scope; }
        }

        #endregion

        #region private

        private ScopeTypes m_Scope;

        private ScopeTypes controllers;

        #endregion
    }
}
